package test;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import java.io.File;
import java.io.IOException;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;

// jsoup demo
public class JsoupDemo {
    public static void main(String[] args) throws IOException {
        File file = new File("/Users/xujiangfei/Downloads/my/html/index.html");
        Document doc = Jsoup.parse(file, "UTF-8");

        Elements media = doc.select("[src]");
        Elements imports = doc.select("link[href]");
        Elements links = doc.select("a[href]");


        print("\nImports: (%d)", imports.size());
        for (Element link : imports) {
            print(" * %s <%s> (%s)", link.tagName(), link.attr("href"), link.attr("rel"));
        }

        print("\nMedia: (%d)", media.size());
        for (Element src : media) {
            if (src.tagName().equals("img"))
                print(" * %s: <%s> %sx%s (%s)",
                        src.tagName(), src.attr("src"), src.attr("width"), src.attr("height"),
                        trim(src.attr("alt"), 20));
            else
                print(" * %s: <%s>", src.tagName(), src.attr("src"));
        }

        print("\nLinks: (%d)", links.size());
        for (Element link : links) {
            print(" * a: <%s>  (%s)", link.attr("href"), trim(link.text(), 35));
        }

    }

    private static void print(String msg, Object... args) {
        System.out.println(String.format(msg, args));
    }

    private static String trim(String s, int width) {
        if (s.length() > width)
            return s.substring(0, width - 1) + ".";
        else
            return s;
    }

    /**
     * 从html中获取出css文本
     * 参考extractCss网站
     * 借助于Js
     * @param html    含有style格式的html文本
     * @return        提取出的css文本
     */
    public static String extractCss(String html){
        Set<String> allClasses = new HashSet<String>();   // 初始化参数
        StringBuffer resultCss = new StringBuffer();
        org.jsoup.nodes.Document doc = Jsoup.parse(html);       //获取html对象
        Elements classes = doc.select("*[class]");
        for(Iterator<Element> ite = classes.iterator(); ite.hasNext();){
            Element element = ite.next();
            String splits[] = element.attr("class").split(" ");
            for(int i=0; i< splits.length; i++){
                allClasses.add(splits[i]);
                if(i==splits.length-1){
                    Elements children = doc.select("."+splits[i]+"> *");
                    for(Iterator<Element> tor = children.iterator(); tor.hasNext();){
                        Element e = tor.next();
                        if(!e.hasAttr("class")){                   //如果没有定义class属性
                            allClasses.add(splits[i]+" > "+e.tagName().toLowerCase());
                        }
                    }
                }
            }
        }
        for(String clss: allClasses){
            Elements element = doc.select("."+clss);
            if(element.hasAttr("style")){
                resultCss.append("."+clss+"{"+element.attr("style")+"}");
            }
        }
        return resultCss.toString();
    }

}
